package database;


import models.User;
import utils.MD5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class score {

    private int flag;

    public int getFlag() {
        return flag;
    }

    public User WriteScore(String name, int score){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        User u  =null;
        try{
            conn  = DBUtil.getConnection();
            String Sql = "insert into score(name,score)values('"+name+"','"+score+"')";
            stmt = conn.prepareStatement(Sql);
            flag=stmt.executeUpdate(Sql);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.closeAll(rs, stmt, conn);
        }
        return u;
    }
    public User SearchScore(String name){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        User u  =null;
        try{
            conn  = DBUtil.getConnection();
            String Sql = "select * from score where name=?";
            stmt = conn.prepareStatement(Sql);
            stmt.setString(1,name);
            rs  = stmt.executeQuery();
            if(rs.next())
            {
                u  =  new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setSocre(rs.getInt(3));
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.closeAll(rs, stmt, conn);
        }
        System.out.print(u);
        return u;
    }
}
